package com.example.handheld4_gsignal_app.services;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import com.example.handheld4_gsignal_app.data.models.NetworkData;

/**
 * 网络服务类 - 处理网络信息获取
 */
public class NetworkService {
    
    private Context context;
    private TelephonyManager telephonyManager;
    private ConnectivityManager connectivityManager;
    private NetworkCallback callback;

    public interface NetworkCallback {
        void onNetworkInfoUpdate(NetworkData networkData);
        void onSignalStrengthUpdate(int signalStrength);
        void onNetworkError(String error);
    }

    public NetworkService(Context context) {
        this.context = context;
        this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        this.connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    /**
     * 获取网络类型
     */
    public String getNetworkType() {
        if (connectivityManager != null) {
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                int type = networkInfo.getType();
                if (type == ConnectivityManager.TYPE_WIFI) {
                    return "WiFi";
                } else if (type == ConnectivityManager.TYPE_MOBILE) {
                    return getMobileNetworkType();
                }
            }
        }
        return "未知";
    }

    /**
     * 获取移动网络类型
     */
    private String getMobileNetworkType() {
        if (telephonyManager != null) {
            int networkType = telephonyManager.getNetworkType();
            switch (networkType) {
                case TelephonyManager.NETWORK_TYPE_LTE:
                    return "4G";
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    return "3G";
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_GPRS:
                    return "2G";
                default:
                    return "移动网络";
            }
        }
        return "移动网络";
    }

    /**
     * 获取运营商名称
     */
    public String getOperatorName() {
        if (telephonyManager != null) {
            String operatorName = telephonyManager.getNetworkOperatorName();
            if (operatorName == null || operatorName.isEmpty()) {
                return "未知运营商";
            }
            // 确保字符编码正确
//            try {
//                // 重新编码确保中文字符正确显示
//                byte[] bytes = operatorName.getBytes("ISO-8859-1");
//                return new String(bytes, "UTF-8");
//            } catch (Exception e) {
//                // 如果编码转换失败，返回原始字符串
//                return operatorName;
//            }
            return operatorName;
        }
        return "未知运营商";
    }

    /**
     * 获取网络运营商代码
     */
    public String getNetworkOperator() {
        if (telephonyManager != null) {
            String operator = telephonyManager.getNetworkOperator();
            return operator != null ? operator : "未知";
        }
        return "未知";
    }

    /**
     * 获取SIM卡运营商代码
     */
    public String getSimOperator() {
        if (telephonyManager != null) {
            String simOperator = telephonyManager.getSimOperator();
            return simOperator != null ? simOperator : "未知";
        }
        return "未知";
    }

    /**
     * 检查网络连接状态
     */
    public boolean isNetworkConnected() {
        if (connectivityManager != null) {
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            return networkInfo != null && networkInfo.isConnected();
        }
        return false;
    }

    /**
     * 获取完整的网络信息
     */
    public String getNetworkInfo() {
        String networkType = getNetworkType();
        String operatorName = getOperatorName();
        return networkType + " - " + operatorName;
    }

    /**
     * 开始网络监控
     */
    public void startNetworkMonitoring(NetworkCallback callback) {
        this.callback = callback;
        // 这里可以添加网络状态监听逻辑
        // 目前先模拟网络信息更新
        if (callback != null) {
            NetworkData networkData = new NetworkData();
            networkData.setNetworkType(getNetworkType());
            networkData.setOperatorName(getOperatorName());
            networkData.setNetworkOperator(getNetworkOperator());
            callback.onNetworkInfoUpdate(networkData);
        }
    }

    /**
     * 停止网络监控
     */
    public void stopNetworkMonitoring() {
        this.callback = null;
    }
}